<?php
if (!$alimama_deluxe->have_user || !$alimama_deluxe->have_this_site) {
	wp_die("Alimama Deluxe 插件尚未正确与 Alimama 帐号关联，请到 <a href=\"admin.php?page=alimama-deluxe/alimama-deluxe-index.php\">Alimama Deluxe 首页</a>完成关联。");
}
	$action = strtolower($_GET['a']);
	if (isset($_POST['zone_add_submit'])) {
		if (empty($_POST['weekprice'])) $_POST['weekprice'] = 1;
		if (empty($_POST['keywords'])) $_POST['keywords']='deluxe';
		$r = $alimama_deluxe->api->zone_add($_POST['name'],$_POST['transtype'],$_POST['weekprice'],$_POST['format'],$_POST['sizecode'],$_POST['needauditing'],$_POST['homepage'],implode(",",$_POST['adzonecatids']),$_POST['keywords'],$_POST['zonedesc']);
		$action = 'manage';
	} elseif (isset($_POST['zone_edit_submit'])) {
		if (empty($_POST['weekprice'])) $_POST['weekprice'] = 1;
		if (empty($_POST['keywords'])) $_POST['keywords']='deluxe';
		$r = $alimama_deluxe->api->zone_edit($_POST['adzoneid'],$_POST['name'],$_POST['weekprice'],$_POST['format'],$_POST['needauditing'],$_POST['homepage'],$_POST['keywords'],$_POST['zonedesc']);
		$action = 'manage';
	}
	if (!in_array($action,array('view','manage','add','edit'))) $action = 'manage';
	if ($action == 'edit' || $action == 'view') {
		$adzoneid = $_GET['id'];
		if (!is_numeric($adzoneid)) wp_die("ERROR ERROR");
		$r = $alimama_deluxe->api->zone_info($adzoneid);
		if ($r && $alimama_deluxe->api->success) {
			$adzone = &$alimama_deluxe->api->result;
		} else {
			wp_die($alimama_deluxe->api->error['desc']);
		}
	}
	$types = new alimama_adzone_types;
	
?>
<div class="wrap">
<?php
	switch ($action) {
		case 'add':
		case 'edit':
			?>
				<h2><?php if ($action == 'add') { ?>添加广告位<a class="helplink" href="http://alimama-deluxe.wordpresscn.com/create-alimama-ad-zone/" target="_blank">[?]</a><?php } else { ?>编辑广告位<a class="helplink" href="http://alimama-deluxe.wordpresscn.com/edit-alimama-adzone/" target="_blank">[?]</a><?php } ?></h2>
				<script type="text/javascript">
					function showhidetranstype() {
						var o = jQuery("select[@name='transtype']").get(0).value;
						if (o == 2 || o == 3) {
							jQuery(".autohide_transtype").show();
						} else jQuery(".autohide_transtype").hide();
					}
					function check_adzonecatids(checkonly) {
						<?php
							if ($action == 'edit') echo "return 1;";
						?>
						count = 0;
						jQuery(".chk_adzonecatids").each(function(){
							if (this.checked) count++;
						});
						if (count > 2) {
							if (!checkonly) alert('您最多只能同时指定两个类别');
							return -1;
						} else if (count < 1) {
							if (!checkonly) alert('您至少选择一个类别');
							return -2;
						} else return 1;
					}
						function checkformsubmit(){
							var errorstr = "";
							var obj = jQuery("input[@name='name']");
							if (obj.get(0).value.length < 1 || obj.get(0).value.length > 16) errorstr += "广告位名称不能为空，或超过 16 个字符；\n";
							obj =  jQuery("select[@name='transtype']").get(0).value;
							if (obj == 2 || obj ==3) {
								obj2 = jQuery("input[@name='weekprice']").get(0).value;
								if (obj2.match(/^\d+$/) == null) errorstr += "周价格必须为有效的数字；\n";
							}
							obj = jQuery("input[@name='keywords']");
							if (obj.get(0).value.length >8) errorstr += "关键字不要超过 8 个字符；\n";
							obj = check_adzonecatids(true);
							switch (obj) {
								case -1: errorstr += "您最多只能指定两个分类；\n";break;
								case -2: errorstr += "您最少要指定一个分类；\n";break;
							}
							obj = jQuery("textarea[@name='zonedesc']");
							if (obj.get(0).value.length<1) errorstr += "广告描述不能留空；\n";
							if (errorstr.length) {
								alert(errorstr);
								return false;
							} else return true;
						}
					
					jQuery('document').ready(function(){
						jQuery("select[@name='transtype']").change(function(){showhidetranstype();});
						jQuery(".chk_adzonecatids").click(function(){
							r = check_adzonecatids();
							if (r==-1) this.checked = false;
							if (r==-2) this.checked = true;
						});
						showhidetranstype();
					});
				</script>
				<form id="zone_info_form" method="post" onsubmit="return checkformsubmit();">
				<table class="optiontable">
					<?php if ($action == 'edit') { ?><input type="hidden" name="adzoneid" value="<?php echo $adzone['adzoneid'];?>" /><?php } ?>
					<tr>
						<th scope="row">广告位名称<sup style="color:red;">*</sup></th>
						<td><input type="text" name="name" value="<?php echo $adzone['name'];?>" size="40" maxlength="16" /></td>
					</tr>
					<tr>
						<th scope="row">广告位形式<sup style="color:red;">*</sup></th>
						<td><select name="format"><?php
							foreach ($types->format as $_k=>$_v) {
								printf('<option value="%s"%s>%s</option>',$_k,($_k==$adzone['format'])?' selected':"",$_v);
							}
						?></select></td>
					</tr>
					<?php if ($action == 'add') { ?><tr>
						<th scope="row">广告位尺寸<sup style="color:red;">*</sup></th>
						<td><select name="sizecode"><?php
							foreach ($types->sizecode as $_k=>$_v) {
								printf('<option value="%s"%s>%s</option>',$_k,($_k==$adzone['sizecode'])?' selected':"",$_v);
							}
						?></select></td>
					</tr><?php } ?>
					<tr<?php if ($action == 'edit') echo " style=\"display:none\"";?>>
						<th scope="row">广告交易类型<sup style="color:red;">*</sup></th>
						<td><select name="transtype"><?php
							foreach ($types->transtype as $_k=>$_v) {
								printf('<option value="%s"%s>%s</option>',$_k,($_k==$adzone['transtype'])?' selected':"",$_v);
							}
						?></select></td>
					</tr>
					<tr class="autohide_transtype" style="display:none">
						<th scope="row">包周价格<sup style="color:red;">*</sup></th>
						<td><input type="text" name="weekprice" value="<?php echo $adzone['weekprice'];?>" size="4" /> 元</td>
					</tr>
					<tr class="autohide_transtype" style="display:none">
						<th scope="row">是否需要审核买家广告<sup style="color:red;">*</sup></th>
						<td><select name="needauditing"><?php
							foreach ($types->needauditing as $_k=>$_v) {
								printf('<option value="%s"%s>%s</option>',$_k,($_k==$adzone['needauditing'])?' selected':"",$_v);
							}
						?></select></td>
					</tr>
					<tr>
						<th scope="row">是否显示于首页<sup style="color:red;">*</sup></th>
						<td><select name="homepage"><?php
							foreach ($types->homepage as $_k=>$_v) {
								printf('<option value="%s"%s>%s</option>',$_k,($_k==$adzone['homepage'])?' selected':"",$_v);
							}
						?></select></td>
					</tr>
					<tr>
						<th scope="row">关键字</th>
						<td><input type="text" name="keywords" value="<?php echo $adzone['keywords'];?>" size="40" maxlength="8" /> 请使用半角逗号分隔</td>
					</tr>
					<?php if ($action == 'add') { ?><tr valign="top">
						<th scope="row">广告位分类<sup style="color:red;">*</sup></th>
						<td>以下分类最多同时选中两个。（注：由于阿里妈妈API 本身的缺陷，目前无法在 Alimama Deluxe 中设定分类[均自动变为其它]，但未来此功能可能会生效）
							<div>
								<script type="text/javascript">
								function shh_adzonecatstable(show) {
									if (show) {
										jQuery('#adzonecatstable').show();
									} else {
										jQuery('#adzonecatstable').hide();
									}
								}
								</script>
								<input type="radio" name="shh_adzonecats" value="1" checked onclick="shh_adzonecatstable(false);" /><strong>隐藏分类详细设置，并默认设定为“其他=>综合/其它”（推荐，您可今后在阿里妈妈后台中改变分类）</strong><br />
								<input type="radio" name="shh_adzonecats" value="0" onclick="shh_adzonecatstable(true);" />显示详细分类设定（不推荐）
								
							</div>
							<table id="adzonecatstable" width="490px" style="display:none"><?php
							$cats = explode(",",$adzone['adzonecatids']);
							if (empty($_POST)) $cats = array('13399');
							foreach ($types->adzonecatids as $___cat) {
								echo "<tr><th valign=\"top\">" . $___cat['name'] . "</th><td><table width=\"380px\">";
								$__i = 0;
								foreach ($___cat['sub'] as $_k=>$_v) {
									if ($__i%3==0) echo "<tr>";
									printf('<td style="padding:0"><input type="checkbox" name="adzonecatids[]" class="chk_adzonecatids" value="%s"%s />%s</td>',$_k,in_array($_k,$cats)?" checked":"",$_v);
									$__i++;
									if ($__i%3==0) echo "</tr>";
								}
								echo "</table></td></tr>";
							}
						?></table>
						</td><!-- need to do -->
					</tr><?php } ?>
					<tr valign="top">
						<th scope="row">广告位描述<sup style="color:red;">*</sup></th>
						<td><textarea cols="60" rows="8" name="zonedesc"><?php echo $adzone['zonedesc'];?></textarea></td>
					</tr>
					<tr>
						<td colspan="2"><p class="submit" style="width: 85%">
						<input type="submit" name="zone_<?php echo $action;?>_submit" value="提交" />
						</p></td>
					</tr>
				</table>
				</form>
			<?php
			break;
		case 'view':
			?>
				<h2>广告位详情<a class="helplink" href="http://alimama-deluxe.wordpresscn.com/view-alimama-adzone/" target="_blank">[?]</a></h2>
				<table class="optiontable">
					<tr>
						<th scope="row">ID</th>
						<td><?php echo $adzone['adzoneid'];?></td>
					</tr>
					<tr>
						<th scope="row">广告位名称</th>
						<td><?php echo $adzone['name'];?></td>
					</tr>
					<tr>
						<th scope="row">广告位形式</th>
						<td><?php echo $types->format[$adzone['format']];?></td>
					</tr>
					<tr>
						<th scope="row">广告位尺寸</th>
						<td><?php echo $types->sizecode[$adzone['sizecode']];?></td>
					</tr>
					<tr>
						<th scope="row">广告交易类型</th>
						<td><?php echo $types->transtype[$adzone['transtype']];?></td>
					</tr>
					<?php if ($adzone['transtype'] == 2 || $adzone['transtype'] == 3) { ?><tr>
						<th scope="row">包周价格</th>
						<td><?php echo $adzone['weekprice'];?> 元</td>
					</tr><?php } ?>
					<?php if ($adzone['transtype'] == 2 || $adzone['transtype'] == 3) { ?><tr>
						<th scope="row">是否需要审核买家广告</th>
						<td><?php echo $types->needauditing[$adzone['needauditing']];?></td>
					</tr><?php } ?>
					<tr>
						<th scope="row">广告位状态</th>
						<td><?php echo $types->status[$adzone['status']];?></td>
					</tr>
					<tr>
						<th scope="row">是否显示于首页</th>
						<td><?php echo $types->homepage[$adzone['homepage']];?></td>
					</tr>
					<?php if (!empty($adzone['keywords'])) { ?><tr>
						<th scope="row">关键字</th>
						<td><?php echo $adzone['keywords'];?></td>
					</tr><?php } ?>
					<tr>
						<th scope="row">广告位分类</th>
						<td><?php
							$cats = explode(",",$adzone['adzonecatids']);
							foreach ($cats as $c) {
								echo " " . $types->adzoneid2catdesc($c);
							}
						?></td>
					</tr>
					<tr valign="top">
						<th scope="row">广告位描述</th>
						<td><?php echo $adzone['zonedesc'];?></td>
					</tr>
					<tr>
						<td colspan="2"><p class="submit">
						<input type="button" onclick="location.href='admin.php?page=alimama-deluxe/alimama-deluxe-aliad.php';" value="返回管理页面" />
						<input type="button" onclick="location.href='admin.php?page=alimama-deluxe/alimama-deluxe-aliad.php&a=edit&id=<?php echo $adzone['adzoneid'];?>';" value="编辑当前广告位" />
						</p></td>
					</tr>
				</table>
				<?php if ($alimama_deluxe->advance_mode) { ?>
				<h2>广告运行统计</h2>
				<div id="ad_stat_report" style="padding: 0 15px;">
					正在读取统计信息，请稍候。
				</div>
				<script type="text/javascript">
				jQuery(document).ready(function(){
					jQuery.ajax({
						type: "POST",
						url: "admin.php?page=alimama-deluxe/alimama-deluxe-ajax.php&noheader=1",
						data: "a=adzone-report&zoneid=<?php echo $adzone['adzoneid'];?>",
						success: function(msg){
							jQuery("#ad_stat_report").html(msg);
						}
					});
				});
				</script>
				<?php } ?>
			<?php
			break;
		default:
?>
	<h2>阿里妈妈广告管理<a class="helplink" href="http://alimama-deluxe.wordpresscn.com/category/manual/adzone-manual/" target="_blank">[?]</a></h2>
	<table class="widefat">
		<thead>
			<tr>
				<th scope="col" id="ad_stg_id" width="80px" style="text-align:center">ID</th>
				<th scope="col" id="ad_stg_name">名称</th>
				<th scope="col" id="ad_stg_code" width="100px">广告尺寸</th>
				<th scope="col" id="ad_stg_action" width="100px">当前状态</th>
				<th scope="col" id="ad_stg_ope" colspan="3" style="text-align:center" width="110px">相关操作</th>
			</tr>
		</thead>
		<tbody id="alimama_ads_list">
			<?php 
				$r = $alimama_deluxe->api->zone_info();
				$count = 0;
				if ($r && $alimama_deluxe->api->success) {
					$a = &$alimama_deluxe->api->result;
					if (isset($a['adzoneid'])) {
						$tmp0= array();
						$tmp0[0] = $a;
						$a = $tmp0;
					}
					$this_site_id = get_option("alimama_deluxe_siteid");
					$class = "";
					foreach ($a as $ad) {
						if ($ad['siteid'] != $this_site_id) continue;
						$count++;
						$class = empty($class)?'alternate':"";
						?>
						<tr class="<?php echo $class;?>">
							<th style="text-align:center"><?php echo $ad['adzoneid'];?></th>
							<td><?php echo $ad['name'];?></td>
							<td><?php echo $ad['adzonesize'];?></td>
							<td><?php echo $types->status[$ad['status']];?></td>
							<td><a href="admin.php?page=alimama-deluxe/alimama-deluxe-aliad.php&a=view&id=<?php echo $ad['adzoneid']?>" class="view">查看详情</a></td>
							<td><a href="admin.php?page=alimama-deluxe/alimama-deluxe-aliad.php&a=edit&id=<?php echo $ad['adzoneid']?>" class="edit">编辑</a></td>
							<td><a href="http://www.alimama.com/membersvc/adzone/zoneView.htm?oper=edit&id=<?php echo $ad['adzoneid']?>" target="_blank" class="edit">到阿里妈妈后台修改</a></td>
						</tr>
						<?php
					}
				} ?>
			<?php $class = empty($class)?'alternate':""; ?>
			<tr class="<?php echo $class;?>"><td colspan="7"><a href="admin.php?page=alimama-deluxe/alimama-deluxe-aliad.php&a=add" class="view">新建广告位</a></td></tr>
			<tr><td colspan="6"><p>注：以上仅列出属于本站的广告位，如需列出全部广告位，请访问<a href="http://www.alimama.com/membersvc/adzone/active_zone_list.htm" target="_blank">阿里妈妈广告位管理后台</a>。</p></td></tr>
			<?php 
				
			?>
		</tbody>
	</table>
	
<?php
	}
?>	
</div>